<script setup lang="ts">
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import type { KnowledgeType, KnowledgeParams, KnowledgeList } from '@/types/consult'
import { getKnowledgeList } from '@/services/consult'

// 父组件传递的请求参数 type
const props = defineProps<{
  type: KnowledgeType
}>()

// 请求参数
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10
})

// 上拉加载组件数据
const list = ref<KnowledgeList>([])
const loading = ref(false)
const finished = ref(false)
// 每次触底和第一次进入页面会加载
const onLoad = async () => {
  // 调用接口，获取数据
  const res = await getKnowledgeList(params.value)
  // 累加数据
  list.value.push(...res.data.rows)
  // 关闭正在加载中的状态
  loading.value = false
  // 当前页加一
  params.value.current++
  // 处理加载结束的逻辑
  if (params.value.current > res.data.pageTotal) {
    finished.value = true
  }
  // if (list.value.length > res.data.total) {
  //   finished.value = true
  // }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
